﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sims3Package;

namespace PackageEditor.Helper
{
    public partial class TGIBlockHelper : Form
    {
        internal TGIBlockList TGIList { get; set; }
        private TGIOrder Order;

        public TGIBlockHelper(ref TGIBlockList list, TGIOrder order)
        {
            InitializeComponent();
            this.TGIList = list;
            this.Order = order;
        }

        private void TGIBlockHelper_Load(object sender, EventArgs e)
        {
            this.Icon = Properties.Resources.Icon;

            foreach (var kvp in ResourceForm.nameMap)
                this.combType.Items.Add(string.Format("{0}    0x{1:X8}", kvp.Value, kvp.Key));
            this.combType.Sorted = true;

            foreach (TGIBlock block in this.TGIList) listTGI.Items.Add(block);
            // Hook the events
            EventHandler handler = new EventHandler(this.OnElementChanged);
            this.combType.TextChanged += handler;
            this.txtGroup.TextChanged += handler;
            this.txtInstance.TextChanged += handler;
        }


        void OnElementChanged(object sender, EventArgs e)
        {
            bool test = true;
            test = test ? ResourceForm.UInt32Parse(txtGroup.Text) : false;
            test = test ? ResourceForm.UInt64Parse(txtInstance.Text) : false;
            if (this.combType.Items.IndexOf(this.combType.Text) >= 0) { test = test ? true : false; } else { test = test ? ResourceForm.UInt32Parse(combType.Text) : false; }
            this.btnAdd.Enabled = test;
        }

        private void listTGI_SelectedIndexChanged(object sender, EventArgs e)
        {
            TGIBlock tgi = listTGI.SelectedItem as TGIBlock;
            if (tgi == null) return;
            if (ResourceForm.nameMap.ContainsKey(tgi.ResourceType)) { this.combType.Text = string.Format("{0}    0x{1:X8}", ResourceForm.nameMap[tgi.ResourceType], tgi.ResourceType); } else { this.combType.Text = "0x" + tgi.ResourceType.ToString("X8"); }
            this.txtInstance.Text = "0x" + tgi.ResourceInstance.ToString("X16");
            this.txtGroup.Text = "0x" + tgi.ResourceGroup.ToString("X8");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            TGIBlock tgi = new TGIBlock(this.combType.Items.IndexOf(this.combType.Text) >= 0 ? Convert.ToUInt32(this.combType.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1], 16) : Convert.ToUInt32(this.combType.Text, 16)
            , Convert.ToUInt32(txtGroup.Text, 16), Convert.ToUInt64(txtInstance.Text, 16), this.Order);
            if(listTGI.Items.Contains(tgi)) if(MessageBox.Show("Same TGIBlock has been existed in the list. Continue?", "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == System.Windows.Forms.DialogResult.No) return;
            listTGI.Items.Add(tgi);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            TGIBlock tgi = listTGI.SelectedItem as TGIBlock;
            if (tgi == null) return;
            listTGI.Items.Remove(tgi);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            TGIList = new TGIBlockList(listTGI.Items.Count);
            foreach (TGIBlock tgi in this.listTGI.Items) TGIList.Add(tgi);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
